package com.project;

import org.ini4j.ConfigParser;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;


public abstract class DB_Operator {

    /**
     * Default constructor
     */
    public DB_Operator() {
    }

    /**
     *
     */
    protected Connection con;

    /**
     * @param input 输入
     * @return 返回
     */
    public abstract boolean add(List<String> input);

    /**
     * @param input 输入
     * @return 返回
     */
    public abstract boolean delete(List<String> input);

    /**
     * @param input 输入
     * @return 返回
     */
    public abstract List<String> find(List<String> input);

    /**
     * @param input 输入
     * @return 返回
     */
    public abstract boolean update(List<String> input);

    /**
     *
     */
    public void connect() {
        try {
            DatabaseConfig cfg = readDatabaseConfig();
            String url = String.format("jdbc:mysql://%s:%d/%s",
                    cfg.host, cfg.port, cfg.dbname);
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(url, cfg.user, cfg.password);
            System.out.println("数据库连接成功！");
        } catch (ClassNotFoundException e) {
            System.err.println("数据库驱动未找到！");
            System.err.println("出现的异常：" + e.getMessage());
        } catch (SQLException e) {
            System.err.println("数据库连接失败！");
            System.err.println("出现的异常：" + e.getMessage());
        }
    }

    /**
     *
     */
    public void close() {
        if (con != null) {
            try {
                con.close();
                System.out.println("数据库连接已关闭！");
            } catch (SQLException e) {
                System.err.println("关闭数据库连接时出错！");
                System.err.println("出现的异常：" + e.getMessage());
            }
        }
    }

    /**
     * @return 返回
     */
    protected DatabaseConfig readDatabaseConfig() {
        try (InputStream input = getClass().getClassLoader().getResourceAsStream("Database.ini")) {
            ConfigParser config = new ConfigParser();
            config.read(input);
            return new DatabaseConfig(
                    config.get("database", "host"),
                    Integer.parseInt(config.get("database", "port")),
                    config.get("database", "dbname"),
                    config.get("database", "user"),
                    config.get("database", "password")
            );
        } catch (Exception e) {
            throw new RuntimeException("读取配置文件失败", e);
        }
    }

    /**
     *
     */
    protected static class DatabaseConfig  {

        /**
         * @param host IP
         * @param port 端口
         * @param dbname 数据库
         * @param user 数据库用户
         * @param password 密码
         */
        public DatabaseConfig(String host, int port, String dbname, String user, String password) {
            this.host = host;
            this.port = port;
            this.dbname = dbname;
            this.user = user;
            this.password = password;
        }

        /**
         *
         */
        public String host;

        /**
         *
         */
        public int port;

        /**
         *
         */
        public String dbname;

        /**
         *
         */
        public String user;

        /**
         *
         */
        public String password;
    }
}